/**
 * 0. Project	: com.dnbns.chart 프로젝트
 *
 * 1. FileName	: ChartView.java
 * 2. Package	: com.dnbns.chart
 * 3. 작성자		: 윤화현
 * 4. 작성일		: 2013. 11. 25. 오전 10:02:57
 * 5. Comment	: 
 */

package com.dnbns.chart;

import java.util.ArrayList;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint.Align;
import android.util.AttributeSet;
import android.view.View;

public class ChartView extends View {
	private GraphicalView mGraphicalView = null;
    private Context mContext;
    
    public ChartView(Context context){
        super(context);
        setFocusable(true);
        mContext = context;
    }
    
    public ChartView(Context context, AttributeSet attrs){
        super(context,attrs);
        setFocusable(true);
        mContext = context;
    }
    
    public ChartView(Context context, AttributeSet attrs, int defaultStyle){
        super(context, attrs, defaultStyle);
        setFocusable(true);
        mContext = context;
    }

    public void makeBarChart(){
    	// 각 계열의 제목.
        String[] titles = new String[] { "2007" };
		// 각 계열의 값들.
		List<double[]> values = new ArrayList<double[]>();

		double[] dd = new double[31];
		for(int i=0; i < 31; i++){
			dd[i] = i*100;
		}
		
		values.add(dd);
		
		// 각 계열의 색깔.
		int[] colors = new int[] { Color.BLUE };

		// renderer 객체 생성.
		XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);
		setChartSettings(renderer, "Monthly sales in the last 2 years",
				"Month", "Units sold", 0.5, 12.5, 0, 24000, Color.GRAY,
				Color.LTGRAY);

		renderer.getSeriesRendererAt(0).setDisplayChartValues(true);
		renderer.setXLabels(12);
		renderer.setYLabels(10);
		renderer.setXLabelsAlign(Align.LEFT);
		renderer.setYLabelsAlign(Align.LEFT);
		renderer.setPanEnabled(true, false);
//		 renderer.setZoomEnabled(false);
		renderer.setZoomRate(1.1f);
		renderer.setBarSpacing(0.5f);
		
		mGraphicalView = ChartFactory.getBarChartView(mContext, buildBarDataset(titles, values), renderer, Type.DEFAULT);

        //뷰에 다시 그리기
//        invalidate();
    }

    //Dataset 정리
	protected XYMultipleSeriesDataset buildBarDataset(String[] titles,
			List<double[]> values) {
		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
		int length = titles.length;
		for (int i = 0; i < length; i++) {
			CategorySeries series = new CategorySeries(titles[i]);
			double[] v = values.get(i);
			int seriesLength = v.length;
			for (int k = 0; k < seriesLength; k++) {
				series.add(v[k]);
			}
			dataset.addSeries(series.toXYSeries());
		}
		return dataset;
	}

    //Renderer 객체 생성및 각 값 지정.
	protected XYMultipleSeriesRenderer buildBarRenderer(int[] colors) {
		XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
		// text사이즈 지정.
		renderer.setAxisTitleTextSize(16);
		renderer.setChartTitleTextSize(20);
		renderer.setLabelsTextSize(15);
		renderer.setLegendTextSize(15);
		
		// 여러색깔 지정하기.
		int length = colors.length;
		for (int i = 0; i < length; i++) {
			SimpleSeriesRenderer r = new SimpleSeriesRenderer();
			r.setColor(colors[i]);
			renderer.addSeriesRenderer(r);
		}
		return renderer;
	}
    
    //차트의 기본적인 셋팅.
	protected void setChartSettings(XYMultipleSeriesRenderer renderer,
			String title, String xTitle, String yTitle, double xMin,
			double xMax, double yMin, double yMax, int axesColor,
			int labelsColor) {
		renderer.setChartTitle(title);	//차트제목		
		renderer.setXTitle(xTitle);		//X 타이틀
		renderer.setYTitle(yTitle);		//Y 타이틀
		renderer.setXAxisMin(xMin);
		renderer.setXAxisMax(xMax);
		renderer.setYAxisMin(yMin);
		renderer.setYAxisMax(yMax);
		renderer.setAxesColor(axesColor);
		renderer.setLabelsColor(labelsColor);
	}

	
    @Override
    protected void onDraw(Canvas canvas) {
    	if(mGraphicalView != null)
    		mGraphicalView.draw(canvas);
    }
}
